
<script type="text/html" data-template-name="file">
    <div class="form-row node-input-filename">
         <label for="node-input-filename"><i class="fa fa-file"></i> <span data-i18n="file.label.filename"></span></label>
         <input id="node-input-filename" type="text">
         <input type="hidden" id="node-input-filenameType">
    </div>
    <div class="form-row">
        <label for="node-input-overwriteFile"><i class="fa fa-random"></i> <span data-i18n="file.label.action"></span></label>
        <select type="text" id="node-input-overwriteFile" style="width: 250px;">
            <option value="false" data-i18n="file.action.append"></option>
            <option value="true" data-i18n="file.action.overwrite"></option>
            <option value="delete" data-i18n="file.action.delete"></option>
        </select>
    </div>
    <div class="form-row form-row-file-write-options">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-appendNewline" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-appendNewline" style="width: 70%;"><span data-i18n="file.label.addnewline"></span></label>
    </div>
    <div class="form-row form-row-file-write-options">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-createDir" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-createDir" style="width: 70%;"><span data-i18n="file.label.createdir"></span></label>
    </div>
    <div class="form-row form-row-file-encoding">
        <label for="node-input-encoding"><i class="fa fa-flag"></i> <span data-i18n="file.label.encoding"></span></label>
        <select type="text" id="node-input-encoding" style="width: 250px;">
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
        <input type="text" id="node-input-name">
    </div>
    <div class="form-tips"><span data-i18n="file.tip"></span></div>
</script>

<script type="text/html" data-template-name="file in">
    <div class="form-row">
         <label for="node-input-filename"><i class="fa fa-file"></i> <span data-i18n="file.label.filename"></span></label>
         <input id="node-input-filename" type="text">
         <input type="hidden" id="node-input-filenameType">
    </div>
    <div class="form-row">
        <label for="node-input-format"><i class="fa fa-sign-out"></i> <span data-i18n="file.label.outputas"></span></label>
        <select id="node-input-format" style="width: 250px;">
            <option value="utf8" data-i18n="file.output.utf8"></option>
            <option value="lines" data-i18n="file.output.lines"></option>
            <option value="" data-i18n="file.output.buffer"></option>
            <option value="stream" data-i18n="file.output.stream"></option>
        </select>
    </div>
    <div class="form-row" id="file-allprops">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-allProps" style="display:inline-block; width:auto; vertical-align:top;">
        <label for="node-input-allProps" style="width: 70%;"><span data-i18n="file.label.allProps"></span></label>
    </div>
    <div class="form-row" id="encoding-spec">
        <label for="node-input-encoding"><i class="fa fa-flag"></i> <span data-i18n="file.label.encoding"></span></label>
        <select type="text" id="node-input-encoding" style="width:250px;">
        </select>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
        <input type="text" id="node-input-name">
    </div>
    <div class="form-tips"><span data-i18n="file.tip"></span></div>
</script>

<script type="text/javascript">
(function(){
    var encodings = [
        [ "file.encoding.native",
          "utf8",
          "ucs2",
          "utf-16le",
          "ascii",
          "binary",
          "base64",
          "hex"
        ],
        [ "file.encoding.unicode",
          "utf-16be",
        ],
        [ "file.encoding.japanese",
          "Shift_JIS",
          "Windows-31j",
          "Windows932",
          "EUC-JP"
        ],
        [ "file.encoding.chinese",
          "GB2312",
          "GBK",
          "GB18030",
          "Windows936",
          "EUC-CN"
        ],
        [ "file.encoding.korean",
          "KS_C_5601",
          "Windows949",
          "EUC-KR"
        ],
        [ "file.encoding.taiwan",
          "Big5",
          "Big5-HKSCS",
          "Windows950"
        ],
        [ "file.encoding.windows",
          "cp874",
          "cp1250",
          "cp1251",
          "cp1252",
          "cp1253",
          "cp1254",
          "cp1255",
          "cp1256",
          "cp1257",
          "cp1258"
        ],
        [ "file.encoding.iso",
          "ISO-8859-1",
          "ISO-8859-2",
          "ISO-8859-3",
          "ISO-8859-4",
          "ISO-8859-5",
          "ISO-8859-6",
          "ISO-8859-7",
          "ISO-8859-8",
          "ISO-8859-9",
          "ISO-8859-10",
          "ISO-8859-11",
          "ISO-8859-12",
          "ISO-8859-13",
          "ISO-8859-14",
          "ISO-8859-15",
          "ISO-8859-16"
        ],
        [ "file.encoding.ibm",
          "cp437",
          "cp737",
          "cp775",
          "cp808",
          "cp850",
          "cp852",
          "cp855",
          "cp856",
          "cp857",
          "cp858",
          "cp860",
          "cp861",
          "cp866",
          "cp869",
          "cp922",
          "cp1046",
          "cp1124",
          "cp1125",
          "cp1129",
          "cp1133",
          "cp1161",
          "cp1162",
          "cp1163"
        ],
        [ "file.encoding.mac",
          "maccroatian",
          "maccyrillic",
          "macgreek",
          "maciceland",
          "macroman",
          "macromania",
          "macthai",
          "macturkish",
          "macukraine",
          "maccenteuro",
          "macintosh"
        ],
        [ "file.encoding.koi8",
          "koi8-r",
          "koi8-u",
          "koi8-ru",
          "koi8-t"
        ],
        [ "file.encoding.misc",
          "armscii8",
          "rk1048",
          "tcvn",
          "georgianacademy",
          "georgianps",
          "pt154",
          "viscii",
          "iso646cn",
          "iso646jp",
          "hproman8",
          "tis620"
        ]
    ];

    RED.nodes.registerType('file',{
        category: 'storage',
        defaults: {
            name: {value:""},
            filename: {value:"", validate: RED.validators.typedInput({ typeField: 'filenameType' })},
            filenameType: {value:"str"},
            appendNewline: {value:true},
            createDir: {value:false},
            overwriteFile: {value:"false"},
            encoding: {value:"none"}
        },
        color:"BurlyWood",
        inputs:1,
        outputs:1,
        icon: "file-out.svg",
        label: function() {
            var fn = this.filename;
            if(this.filenameType != "str" && this.filenameType != "env" ) { fn = ""; }
            if(this.filenameType === "env") { fn = "env."+fn; }
            if (this.overwriteFile === "delete") {
                return this.name||this._("file.label.deletelabel",{file:fn});
            } else {
                return this.name||fn||this._("file.label.write");
            }
        },
        paletteLabel: RED._("node-red:file.label.write"),
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        oneditprepare: function() {
            var node = this;
            var encSel = $("#node-input-encoding");
            var label = node._("file.encoding.none");
            $("<option/>", {
                value: "none",
                label: label
            }).text(label).appendTo(encSel);
            $("<option/>", {
                value: "setbymsg",
                label: node._("file.encoding.setbymsg")
            }).text(label).appendTo(encSel);
            $("#node-input-filename").typedInput({
                default: "str",
                types: [{label:RED._("node-red:file.label.path"), value:"str", icon:""}, "msg", "jsonata", "env"],
                typeField: $("#node-input-filenameType")
            });
            if(typeof node.filenameType == 'undefined') {
                //existing node AND filenameType is not set - inplace (compatible) upgrade to new typedInput
                if(node.filename == "") { //was using empty value to denote msg.filename - set typedInput to match
                    node.filename = "filename";
                    node.filenameType = "msg";
                    $("#node-input-filename").typedInput("type", node.filenameType);
                    $("#node-input-filename").typedInput("value", node.filename);
                } else if(/^\${[^}]+}$/.test(node.filename)) { //was using an ${ENV_VAR}
                    node.filenameType = "env";
                    node.filename = node.filename.replace(/\${([^}]+)}/g, function(match, name) {
                        return (name === undefined)?"":name;
                    });
                    $("#node-input-filename").typedInput("type", node.filenameType);
                    $("#node-input-filename").typedInput("value", node.filename);
                } else { //was using a static filename - set typedInput type to str
                    node.filenameType = "str";
                    $("#node-input-filename").typedInput("type", node.filenameType);
                    $("#node-input-filename").typedInput("value", node.filename);
                }
            }
            encodings.forEach(function(item) {
                if(Array.isArray(item)) {
                    var group = $("<optgroup/>", {
                        label: node._(item[0])
                    }).appendTo(encSel);
                    for (var i = 1; i < item.length; i++) {
                        var enc = item[i];
                        $("<option/>", {
                            value: enc,
                            label: enc
                        }).text(enc).appendTo(group);
                    }
                }
                else {
                    $("<option/>", {
                        value: item,
                        label: item
                    }).text(item).appendTo(encSel);
                }
            });
            encSel.val(node.encoding);
            $("#node-input-overwriteFile").on("change",function() {
                if (this.value === "delete") {
                    $(".form-row-file-write-options").hide();
                    $(".form-row-file-encoding").hide();
                } else {
                    $(".form-row-file-write-options").show();
                    $(".form-row-file-encoding").show();
                }
            });
        }
    });

    RED.nodes.registerType('file in',{
        category: 'storage',
        defaults: {
            name: {value:""},
            filename: {value:"", validate: RED.validators.typedInput({ typeField: 'filenameType' }) },
            filenameType: {value:"str"},
            format: {value:"utf8"},
            chunk: {value:false},
            sendError: {value: false},
            encoding: {value: "none"},
            allProps: {value: false}
        },
        color:"BurlyWood",
        inputs:1,
        outputs:1,
        outputLabels: function(i) {
            var l;
            if (this.format === "utf8") {
                l = "file.label.utf8String";
            } else if (this.format === "lines") {
                l = "file.label.utf8String_plural";
            } else if (this.format === "stream") {
                l = "file.label.binaryBuffer_plural";
            } else {
                l = "file.label.binaryBuffer";
            }
            return this._(l);
        },
        icon: "file-in.svg",
        label: function() {
            var fn = this.filename;
            if(this.filenameType != "str" && this.filenameType != "env" ) { fn = ""; }
            if(this.filenameType === "env") { fn = "env."+fn; }
            return this.name||fn||this._("file.label.read");
        },
        paletteLabel: RED._("node-red:file.label.read"),
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        oneditprepare: function() {
            var node = this;
            var encSel = $("#node-input-encoding");
            var label = node._("file.encoding.none");
            $("<option/>", {
                value: "none",
                label: label
            }).text(label).appendTo(encSel);
            $("#node-input-filename").typedInput({
                default: "str",
                types: [{label:RED._("node-red:file.label.path"), value:"str", icon:""}, "msg", "jsonata", "env"],
                typeField: $("#node-input-filenameType")
            });
            if(typeof node.filenameType == 'undefined') {
                //existing node AND filenameType is not set - inplace (compatible) upgrade to new typedInput
                if(node.filename == "") { //was using empty value to denote msg.filename - set typedInput to match
                    node.filename = "filename";
                    node.filenameType = "msg";
                    $("#node-input-filename").typedInput("type", node.filenameType);
                    $("#node-input-filename").typedInput("value", node.filename);
                } else if(/^\${[^}]+}$/.test(node.filename)) { //was using an ${ENV_VAR}
                    node.filenameType = "env";
                    node.filename = node.filename.replace(/\${([^}]+)}/g, function(match, name) {
                        return (name === undefined)?"":name;
                    });
                    $("#node-input-filename").typedInput("type", node.filenameType);
                    $("#node-input-filename").typedInput("value", node.filename);
                } else { //was using a static filename - set typedInput type to str
                    node.filenameType = "str";
                    $("#node-input-filename").typedInput("type", node.filenameType);
                    $("#node-input-filename").typedInput("value", node.filename);
                }
            }
            encodings.forEach(function(item) {
                if(Array.isArray(item)) {
                    var group = $("<optgroup/>", {
                        label: node._(item[0])
                    }).appendTo(encSel);
                    for (var i = 1; i < item.length; i++) {
                        var enc = item[i];
                        $("<option/>", {
                            value: enc,
                            label: enc
                        }).text(enc).appendTo(group);
                    }
                }
                else {
                    $("<option/>", {
                        value: item,
                        label: item
                    }).text(item).appendTo(encSel);
                }
            });
            encSel.val(node.encoding);
            $("#node-input-format").on("change",function() {
                var format = $("#node-input-format").val();
                if ((format === "utf8") || (format === "lines")) {
                    $("#encoding-spec").show();
                }
                else {
                    $("#encoding-spec").hide();
                }
                if ((format === "lines") || (format === "stream")) {
                    $("#file-allprops").show();
                }
                else {
                    $("#file-allprops").hide();
                }
            });
        }
    });
})();
</script>
